package com.lwj.liwanjia.view.activity.cuxiao;

import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.MainActivity;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.PromotionMoreAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.AddGwcBean;
import com.lwj.liwanjia.bean.PromotionMoreBean;
import com.lwj.liwanjia.impl.AddGwcView;
import com.lwj.liwanjia.impl.PromotionMoreView;
import com.lwj.liwanjia.presenter.AddGwcPresenter;
import com.lwj.liwanjia.presenter.PromotionMorePresenter;
import com.lwj.liwanjia.utils.NetWorkUtil;
import com.lwj.liwanjia.utils.PopUtils;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.lwj.liwanjia.view.activity.supermarket.MarketActivity;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PromotionMoreActivity extends BaseActivity implements PromotionMoreView, SuperRecyclerView.LoadingListener {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.promotion_rlv)
    SuperRecyclerView promotionRlv;
    @BindView(R.id.progress)
    ProgressBar progressBar;

    private PromotionMoreAdapter adapter;
    private List<PromotionMoreBean.DataBean.CommendityBean> list = new ArrayList<>();
    private PromotionMorePresenter morePresenter;
    private int page = 0;
    private int allPage;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        ivRight.setVisibility(View.VISIBLE);
        tvTitle.setText("促销商品");
        ivRight.setImageResource(R.mipmap.fenlei);
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        promotionRlv.setLayoutManager(manager);
        promotionRlv.setRefreshEnabled(true);
        promotionRlv.setLoadMoreEnabled(true);
        promotionRlv.setLoadingListener(this);
        promotionRlv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.left = getResources().getDimensionPixelOffset(R.dimen.x10);
                outRect.bottom = getResources().getDimensionPixelOffset(R.dimen.x10);
                if (parent.getChildLayoutPosition(view) % 2 == 0) {
                    outRect.right = getResources().getDimensionPixelOffset(R.dimen.x10);
                }
            }
        });
        adapter = new PromotionMoreAdapter(this, list);
        promotionRlv.setAdapter(adapter);
        morePresenter = new PromotionMorePresenter(this);
        clickListener();
    }

    private void clickListener() {
        //查看详情
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(PromotionMoreActivity.this, ShangpinDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("shopGoodsCode", list.get(position).getCommodityCode());
                bundle.putString("commodityType", list.get(position).getCommodityType());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        morePresenter.getPromotionMore(page);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_promotion_more;
    }

    @OnClick({R.id.tv_left, R.id.iv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.iv_right://菜单
                PopUtils.getInstance().showPopupWindow(PromotionMoreActivity.this, ivRight);
                break;
        }
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getPromotionMore(PromotionMoreBean promotionMoreBean) {
        if (promotionMoreBean.getState() == 1) {
            allPage = promotionMoreBean.getData().get(0).getAllPage();
            if (page == 0) {
                list.clear();
            }
            list.addAll(promotionMoreBean.getData().get(0).getCommendity());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onRefresh() {
        page = 0;
        initData();
        promotionRlv.completeRefresh();
    }

    @Override
    public void onLoadMore() {
        if (page >= allPage - 1) {
            promotionRlv.setNoMore(true);
            return;
        }
        page++;
        initData();
        promotionRlv.completeLoadMore();
    }
}
